local composer = require("composer")
local physics = require("physics")
local constant = require("constant")
local math = require("math")

local gamescene = composer.newScene()

physics.start()
physics.setGravity(0, 0)

local bgm = audio.loadSound('bgm.mp3')
local bounceSound = audio.loadSound("bounce.mp3")
local leftBar
local rightBar
local ball
local retryText
local gaming = true

local function reload(event)
    local phase = event.phase
    if(phase == 'began')then
        display.getCurrentStage():setFocus(event.target, event.id)
    elseif(phase == 'ended') then
        composer.removeScene("gameScene", true)
        local currentscene = composer.getSceneName("current")
        composer.gotoScene(currentscene, {params = {lb = constant.leftBar,rb = constant.rightBar}})
        display.getCurrentStage():setFocus(nil)
        event.target:removeSelf()
    end
end

local function touchevent(event)
        local phase = event.phase
        local target = event.target
        if(gaming) then
            if("began" == phase) then
                display.getCurrentStage():setFocus(target, event.id)
            elseif("moved" == phase) then
                target.y = event.y
            elseif("ended" == phase) then
                display.getCurrentStage():setFocus(nil)
            end
        end
end

local function winCollition(self, event)
    if(event.other == ball and gaming) then
        self:setFillColor(0.5,0,0)
        if(retryText)then
            retryText.isVisible = true
        end
        gaming = false
        audio.stop()
    end
end

local function bounceBack(self, event)
    local phase = event.phase
    if(event.other == leftBar or event.other == rightBar and phase == 'ended') then
        local vx, vy = ball:getLinearVelocity()
        if(math.sqrt(vx^2 + vy^2) > 600)then
            local len = 600/math.sqrt(vx^2+vy^2)
            vx = vx*len
            vy = vy*len
        else
            vx = vx*1.2
            vy = vy*1.2
        end
        ball:setLinearVelocity(vx, vy) 
        audio.play(bounceSound)
    end
end

function gamescene:create(event)
    local sceneGroup = self.view

    audio.play(bgm, {loops = -1})
    gaming = true
    if(retryText)then
        display.remove(retryText)
    end
    -- bgRect
    local bgRect = display.newRect(0,0, display.contentWidth + 300, display.contentHeight)
    bgRect.x = display.contentCenterX
    bgRect.y = display.contentCenterY
    bgRect:setFillColor(0.3, 0.3, 0.3)
    sceneGroup:insert(bgRect)
    --ball
    ball = display.newCircle(display.contentCenterX, display.contentCenterY, 20)
    ball.isVisible = false
    ball:setFillColor(0.7, 0, 0)
    ball.collision = bounceBack
    ball:addEventListener("collision")
    physics.addBody(ball, "dynamic", {density = 1, friction = 0, bounce = 1})
    sceneGroup:insert(ball)
    -- border
    options = {density = 1, bounce = 0, friction = 0}
    local topBorder = display.newRect(display.contentCenterX, -10, display.contentWidth+100, 10)
    physics.addBody(topBorder, "static", options)
    topBorder:setFillColor(0.2,0.2,0.2)
    local bottomBorder = display.newRect(display.contentCenterX, display.contentHeight + 10, display.contentWidth+100, 10)
    physics.addBody(bottomBorder, "static", options)
    bottomBorder:setFillColor(0.2,0.2,0.2)
    local leftBorder = display.newRect(-100, display.contentCenterY, 10, display.contentHeight+100)
    physics.addBody(leftBorder, "static", options)
    leftBorder:setFillColor(0.2,0.2,0.2)
    leftBorder.collision = winCollition
    leftBorder:addEventListener("collision")
    local rightBorder = display.newRect(display.contentWidth + 100, display.contentCenterY, 10, display.contentHeight+100)
    physics.addBody(rightBorder, "static")
    rightBorder:setFillColor(0.2,0.2,0.2)
    rightBorder.collision = winCollition
    rightBorder:addEventListener("collision")
    sceneGroup:insert(topBorder) 
    sceneGroup:insert(bottomBorder) 
    sceneGroup:insert(leftBorder) 
    sceneGroup:insert(rightBorder) 
    -- get params
    local leftBarInfo = event.params['lb']
    local rightBarInfo = event.params['rb']
    leftBar = display.newRect(leftBarInfo[1], leftBarInfo[2], leftBarInfo[3], leftBarInfo[4])
    leftBar:setFillColor(0, 1, 1)
    physics.addBody(leftBar, "dynamic", {density = 1, friction = 0.2, bounce = 0.2})
    rightBar = display.newRect(rightBarInfo[1]-50, rightBarInfo[2], rightBarInfo[3], rightBarInfo[4])
    rightBar:setFillColor(0, 1, 0)
    physics.addBody(rightBar, "dynamic", {density=1, friction = 0.2, bounce = 0.2})

    leftBar:addEventListener('touch', touchevent)
    rightBar:addEventListener('touch', touchevent)

    sceneGroup:insert(leftBar)
    sceneGroup:insert(rightBar)

    retryText = display.newText("重新开始", display.contentCenterX, display.contentCenterY, native.systemFont, 40)
    retryText:setFillColor(0, 0.5, 0)
    retryText:addEventListener("touch", reload)
    retryText.isVisible = false
    sceneGroup:insert(retryText)

    --tap to start
    local startgameText = display.newText("点击开始发球", display.contentCenterX, display.contentCenterY, native.systemFont, 50)
    startgameText.y = startgameText.y + 100
    startgameText:setFillColor(0, 0, 0)
    startgameText:addEventListener("touch", 
    function(event)
        local phase = event.phase
        if(phase == 'began')then
            display.getCurrentStage():setFocus(event.target, event.id)
        elseif(phase == 'ended') then
            startgameText:setFillColor(1, 1, 1)
            display.remove(startgameText)
            startgameText = nil
            ball.isVisible = true
            ball:setLinearVelocity(300, 300)
            display.getCurrentStage():setFocus(nil)
        end
    end)
    sceneGroup:insert(startgameText)
end

gamescene:addEventListener("create", gamescene)

return gamescene